package com.ihealthway.all.view;

import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.view.AbstractPathView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Implements custom view
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
public class ProxyRedirectView extends AbstractPathView {

    private static final Log logger = Logs.get();


    private PropertiesProxy conf = Mvcs.getIoc().get(PropertiesProxy.class, "conf");

    public ProxyRedirectView(String dest) {
        super(dest);
    }

    @Override
    public void render(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Throwable {
        String path = evalPath(req, obj);

        if (path.startsWith("http://") || path.startsWith("https://")) {}
        // Absolute path, add the context path for it
        else if (path.length() > 0 && path.charAt(0) == '/') {
            path = req.getScheme() + "://" + conf.get("server_domain") + req.getContextPath() + path;
            logger.infof("Proxy URL: %s", path);
        }

        resp.sendRedirect(path);
        resp.flushBuffer();
    }
}
